<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\model\Misspeople;
use App\model\China;

class FindController extends Controller
{
	protected $types = ['1' => "寻老友",'2' => '寻战友','3'=>'家人寻亲','4'=>'台海寻人','5'=>'港澳台寻人','6'=>'亲人寻家'];

    //寻人后台管理
    public function index(Request $request)
    {
    	$label = '寻人发布管理';
    	$types = $this->types;
    	$areas = new China;
    	$area_find = $areas->getAllarea();
        $code = $this->CodeNumber();
    	if ($request->ajax()) {
    		//后台用户列表接口
            $page = $request->page ? $request->page : 1;
            $limit = $request->limit ? $request->limit : 10;
            $offset = ($page - 1) * $limit;
            $count = Misspeople::count('id');
    
            $Findmsg = Misspeople::limit($limit)->offset($offset)->orderBy('id','desc')->get()->toArray();

            if (!empty($Findmsg)) 
            {
                foreach ($Findmsg as $key => &$value) 
                {
                	$value['typename'] = $types[$value['find_type']];
                    $value['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
                    unset($value);
                }
            }
            return $this->return_ajax_data($Findmsg,'','',$count);
    	}
    	return view('admin.findpeople.index',compact('label','types','area_find','code'));
    }

    public function findAdd(Request $request)
    {
    	$data = $request->all();
    	$findadd = new Misspeople;
    	$status = $findadd->AddpeopleAdmin($data);
        
    	if ($status) {
    		return $this->call_back_status($status);
    	}
    }

    public function findUpdate(Request $request)
    {
    	$data = $request->all();    
        $update_data = [
                		'miss_name' => $request['miss_name'],
                		'miss_sex' => $request['miss_sex'],
                		'miss_home' => $request['miss_home'],
                		'miss_barth' => $request['miss_barth'],
                		'miss_high' => $request['miss_high'],
                		'miss_date' => $request['miss_date'],
                		'miss_access' => $request['miss_access'],
                		'maybe_access' => $request['maybe_access'],
                		'money' => $request['miss_money'],
                		'find_type' => $request['find_type'],
                		'miss_face_text' => $request['miss_face'],
                		'miss_experience' => $request['miss_experience'],
                		'other_desc' => $request['others'],
                		'contact_name' => $request['contact_name'],
                		'contact_net' => $request['contact_net'],
                		'contact_phone' => $request['contact_phone'],
                		'contact_email' => $request['contact_email'],
                		'contact_other' => $request['contact_other'],
                		'miss_pic' => $request['peope_pic'],
                		'find_area' => $request['find_area'],
                        'miss_desc' => $request['miss_desc'],
                        // 'video_url' => $request['videourl'],
                        'first_order' => isset($request['first_order']) ? $request['first_order'] : '',

                        ];
        $update_status = Misspeople::where('id','=',$data['id'])->update($update_data);
        return $this->call_back_status($update_status);
    }

    public function changeStatus(Request $request)
    {
    	$data = $request->all();
    	if($data['types'] == 1)
    	{
    		$name = 'is_shelf';
    	}
    	else if($data['types'] == 2)
    	{
    		$name = 'showindex';
    	}
    	else if($data['types'] == 3)
    	{
			$name = 'status';
    	}
    	
        $status = Misspeople::where('id','=',$data['id'])->update([$name=>$data[$name]]);
        return $this->call_back_status($status);
    }

    public function findDel(Request $request)
    {
        if (!$request->has('id')) return response()->json('非法操作',401);
        return $this->call_back_status(Misspeople::destroy($request->id));
    }
}
